package cn.the.angry.tests;

import java.awt.Graphics2D;
import java.util.LinkedList;
import java.util.List;

import org.jbox2d.collision.AABB;
import org.jbox2d.collision.shapes.CircleDef;
import org.jbox2d.collision.shapes.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.DebugDraw;
import org.jbox2d.dynamics.World;
import cn.the.angry.anim.DamageableObject;
import cn.the.angry.anim.PhysicalObject;
import cn.the.angry.resourcemanager.ResourceManager;
import cn.the.angry.util.DrawUtil;

public class SimplePhysicsTest extends AbstractTest
{
//	private final List<PhysicalObject> physicalObjects=new LinkedList<PhysicalObject>();
//	private final Vec2 groundSize;
//	private final Body groundBody;
//	private final DebugDraw m_debugDraw;
//	private final World m_world;
//	private final AABB m_worldAABB;
//
//	public SimplePhysicsTest()
//	{
//		super();
//		m_debugDraw=new DrawUtil(displayBufferImage);
//		m_worldAABB=new AABB();
//		m_worldAABB.lowerBound.set(new Vec2(-10, -10));
//		m_worldAABB.upperBound.set(new Vec2(1000, 1000));
//		m_world=new World(m_worldAABB, new Vec2(0f,10f), true);
//		m_world.setDebugDraw(m_debugDraw);
//		m_world.setWarmStarting(true);
//		m_world.setPositionCorrection(true);
//		m_world.setContinuousPhysics(true);
//		m_debugDraw.setFlags(0);
//		m_debugDraw.appendFlags(DebugDraw.e_shapeBit);
//		m_debugDraw.appendFlags(DebugDraw.e_jointBit);
//		m_debugDraw.appendFlags(DebugDraw.e_coreShapeBit);
//		m_debugDraw.appendFlags(DebugDraw.e_aabbBit);
//		m_debugDraw.appendFlags(DebugDraw.e_obbBit);
//		m_debugDraw.appendFlags(DebugDraw.e_pairBit);
//		m_debugDraw.appendFlags(DebugDraw.e_centerOfMassBit);
//		m_debugDraw.appendFlags(DebugDraw.e_controllerBit);
//		m_debugDraw.setCamera(0, 0, 1f);
//		
//		groundSize=new Vec2(WIDTH, 200);
//		PolygonDef groundPolygonDef = new PolygonDef();
//		groundPolygonDef.setAsBox(groundSize.x/2, groundSize.y/2);
//
//		BodyDef groundBodyDef = new BodyDef();
//		groundBodyDef.position = new Vec2(groundSize.x/2, HEIGHT-groundSize.y/2);
//		groundBody=m_world.createBody(groundBodyDef);
//		groundBody.createShape(groundPolygonDef);
//		
//		for(int i=0; i<6; ++i)
//		{
//			PhysicalObject ball = new PhysicalObject(new DamageableObject(ResourceManager.getMiddleWoodBallImages()),1f);
//			physicalObjects.add(ball);
//			
//			CircleDef testCircleDef=new CircleDef();
//			testCircleDef.density=1.0f;
//			testCircleDef.friction=1.0f;
//			testCircleDef.radius=ball.getFrameWidth()/2;
//			testCircleDef.restitution=0.9f;
//			
//			BodyDef testCircleBodyDef=new BodyDef();
//			testCircleBodyDef.allowSleep=true;
//			testCircleBodyDef.position=new Vec2(240f+80*i, -10f);
//			
//			ball.setObjectBody(m_world.createBody(testCircleBodyDef));
//			ball.getObjectBody().createShape(testCircleDef);
//			ball.getObjectBody().setMassFromShapes();
//			ball.setLinearVelocity(new Vec2(10f*(i%2==0?1:-1), 0.0f));
//		}
//	}
//
//	public void step(float dt, int iterations)
//	{
//		m_world.step(dt, iterations);
//		final Graphics2D g2d=displayBufferImage.createGraphics();
//		Vec2 drawRectPoint1=new Vec2(),drawRectPoint2=new Vec2();
//		final int[] repaintPoint=new int[ physicalObjects.size()*4 ];
//		
//		for(int i=0; i<physicalObjects.size(); ++i)
//		{
//			final PhysicalObject physicalObject=physicalObjects.get(i);
//			if(physicalObject.getData(1)!=null)
//			{
//				final int repaintPoint1X=repaintPoint[i*4]=((Float) physicalObject.getData(1)).intValue();
//				final int repaintPoint1Y=repaintPoint[i*4+1]=((Float) physicalObject.getData(2)).intValue();
//				final int repaintPoint2X=repaintPoint[i*4+2]=((Float) physicalObject.getData(3)).intValue();
//				final int repaintPoint2Y=repaintPoint[i*4+3]=((Float) physicalObject.getData(4)).intValue();
//			
//				final int OFFSET=1;
//				g2d.drawImage(backgroundBufferImage, repaintPoint1X-OFFSET, repaintPoint1Y-OFFSET, 
//						repaintPoint2X+OFFSET, repaintPoint2Y+OFFSET, 
//						repaintPoint1X-OFFSET, repaintPoint1Y-OFFSET, 
//						repaintPoint2X+OFFSET, repaintPoint2Y+OFFSET, null);
//			}
//		}
//		
//		for(int i=0; i<physicalObjects.size(); ++i)
//		{
//			final PhysicalObject physicalObject=physicalObjects.get(i);
//			Integer repaintPoint1X=null,repaintPoint1Y=null,repaintPoint2X=null,repaintPoint2Y=null;
//			if(physicalObject.getData(1)!=null)
//			{
//				repaintPoint1X=repaintPoint[i*4];
//				repaintPoint1Y=repaintPoint[i*4+1];
//				repaintPoint2X=repaintPoint[i*4+2];
//				repaintPoint2Y=repaintPoint[i*4+3];
//			}
//			
//			drawRectPoint1.x=physicalObject.getLocation().x;
//			drawRectPoint1.y=physicalObject.getLocation().y;
//			drawRectPoint2.x=physicalObject.getLocation().x+physicalObject.getFrameWidth();
//			drawRectPoint2.y=physicalObject.getLocation().y+physicalObject.getFrameHeight();
//			
//			m_debugDraw.getWorldToScreenToOut(drawRectPoint1, drawRectPoint1);
//			m_debugDraw.getWorldToScreenToOut(drawRectPoint2, drawRectPoint2);
//			
//			physicalObject.setData(1, drawRectPoint1.x);
//			physicalObject.setData(2, drawRectPoint1.y);
//			physicalObject.setData(3, drawRectPoint2.x);
//			physicalObject.setData(4, drawRectPoint2.y);
//			
//			g2d.drawImage(physicalObject.getImage(), (int) drawRectPoint1.x, (int) drawRectPoint1.y, 
//					(int) (drawRectPoint2.x-drawRectPoint1.x), (int) (drawRectPoint2.y-drawRectPoint1.y), null);
//			
//			if(repaintPoint1X==null || repaintPoint1X>repaintPoint[i*4]) 
//				repaintPoint1X=repaintPoint[i*4];
//			if(repaintPoint1Y==null || repaintPoint1Y>repaintPoint[i*4+1])
//				repaintPoint1Y=repaintPoint[i*4+1];
//			if(repaintPoint2X==null || repaintPoint2X<repaintPoint[i*4+2]) 
//				repaintPoint2X=repaintPoint[i*4+2];
//			if(repaintPoint2Y==null || repaintPoint1Y>repaintPoint[i*4+3]) 
//				repaintPoint2Y=repaintPoint[i*4+3];
//			
//			repaintPoint[4*i]=repaintPoint1X;
//			repaintPoint[4*i+1]=repaintPoint1Y;
//			repaintPoint[4*i+2]=repaintPoint2X;
//			repaintPoint[4*i+3]=repaintPoint2Y;
//		}
//				
//		for(int i=0; i<4*physicalObjects.size(); i+=4)
//		{
//			final int OFFSET=1;
//			
//			g2d.drawImage(foregroundBufferImage, repaintPoint[i]-OFFSET, repaintPoint[i+1]-OFFSET, 
//					repaintPoint[i+2]+OFFSET, repaintPoint[i+3]+OFFSET, 
//					repaintPoint[i]-OFFSET, repaintPoint[i+1]-OFFSET,
//					repaintPoint[i+2]+OFFSET, repaintPoint[i+3]+OFFSET, null);
//			
//			this.getGraphics().drawImage(displayBufferImage, repaintPoint[i]-OFFSET, repaintPoint[i+1]-OFFSET, 
//					repaintPoint[i+2]+OFFSET, repaintPoint[i+3]+OFFSET, 
//					repaintPoint[i]-OFFSET, repaintPoint[i+1]-OFFSET,
//					repaintPoint[i+2]+OFFSET, repaintPoint[i+3]+OFFSET, null);
//		}
//
//		g2d.dispose();
//	}
//	
//	public static void main(String[] args)
//	{
//		final SimplePhysicsTest main = new SimplePhysicsTest();
//        main.setVisible(true);
//        main.getContentPane().getComponent(0).repaint();
//        new Thread(new Runnable()
//        {    
//            @Override
//            public void run()
//            {
//                for(;;)
//                {
//                	try
//					{
//						Thread.sleep(5);
//					}
//					catch (InterruptedException e)
//					{
//						e.printStackTrace();
//					}
//                    final float timeStep=1/60f;
//            		final int iterationCount=10;
//            		main.step(timeStep, iterationCount);            		
//                }
//            }
//            
//        }).start();
//	}
}
